layui.config({
    base: "static/layui/"
}).extend({
    tableSelect: 'tableSelect'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload','tableSelect','laydate'], function () {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.jquery;
    var tableSelect = layui.tableSelect;
    var laydate = layui.laydate;

    form.render()

    // 日期
    laydate.render({
        elem: '#reloadInv',
        max: 0
    });

    // 提交事件
    form.on('submit(demo1)', function(data){
        let index = layer.msg('结转中，请稍后！',{icon: 16,time:false,shade:0.8});
        var field = data.field.reloadInv; // 获取表单字段值
        $.post("manage/inventory/reloadInvSum",{day: field},function (d) {
            if (d.code === 200){
                layer.msg("已经开始结转，日期跨度较长，大约需要30分钟，请在30分钟后重新查询！")
            }else {
                layer.msg(d.msg)
            }
        })
        return false; // 阻止默认 form 跳转
    });

    tableSelect.render({
        elem: '#cusCode',
        checkedKey: 'cusCode',
        searchPlaceholder: '企业编码',
        searchKey: 'cusCode',
        table: {
            url: 'manage/common/list/customer'
            ,method: 'post'
            ,cols: [
                [
                    { type: 'radio' },
                    { field: 'cusCode', title: '企业编码' },
                    { field: 'cusName', title: '企业名称' },
                ]
            ]
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                NEWJSON.push(item.cusCode)
            })
            elem.val(NEWJSON.join(","))

        }
    })

    // tableSelect.render({
    //     elem: '#material',
    //     checkedKey: 'invCode',
    //     searchPlaceholder: '料号编码',
    //     searchKey: 'invCode',
    //     table: {
    //         url: 'base_material/djBaseList',
    //         cols: [
    //             [
    //                 { type: 'radio' },
    //                 { field: 'invCode',hide: true ,
    //                     templet: function (e) {
    //                         return e.djMaterial
    //                     }
    //                 },
    //                 { field: 'djMaterial',width: 150, title: '料号编码' },
    //                 { field: 'cusMaterial',width: 150, title: '客户料号' },
    //                 { field: 'materialMrk',width: 150, title: '料号名称' },
    //                 { field: 'cusCode',width: 100, title: '企业编码' },
    //                 { field: 'cusName',width: 150, title: '企业名称' },
    //             ]
    //         ]
    //     },
    //     done: function (elem, data) {
    //         var NEWJSON = []
    //         layui.each(data.data, function (index, item) {
    //             $("#classType").val(item.classType)
    //             NEWJSON.push(item.djMaterial)
    //         })
    //         elem.val(NEWJSON.join(","))
    //     }
    // })

    table.render({
        elem: '#table'
        ,title: '列表'
        ,method: 'post'
        ,height: 'full-200'
        ,page: true //开启分页
        ,limit: 20
        ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
        ,loading: true
        ,cols: [
            [
                { field: 'order', title: '序号',align:'center',templet: function (data) {
                        return data.LAY_TABLE_INDEX + 1
                    }},
                { field: 'djMaterial', title: '⼚商料号' },
                { field: 'cusMaterial', title: '纬创料号或制程' },
                { field: 'materialMrk', title: '品名或料号描述' },
                { field: 'className', title: '仓库'},
                { field: 'storageSpace', title: '储位'},
                { field: 'batchNumber', title: '批号(批次号)'},
                { field: 'sum', title: '数量'  }
            ]
        ]
        ,text: {
            none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
        }
        ,data: []
        ,toolbar: '#tableToolbar'
        ,defaultToolbar: ['filter']
    });

    table.on('toolbar(table)', function(obj){
        switch (obj.event) {
            case 'find':
                let obj = {
                    cusCode:$('#cusCode').val(),
                    material:$('#material').val(),
                    invDef:$('#cInvDefine9').val()
                }
                let itm = layer.load(0, { shade: 0.3 });
                $.post('/wc_api/getInventoryAgoNext',obj,function (d) {
                    layer.close(itm)
                    table.reload('table', {
                        data: d.data,
                        page: {
                            curr: 1
                        }
                    });
                })
                break;
            case 'download':
                let cusCode = $('#cusCode').val();
                let material = $('#material').val();
                let invDef = $('#cInvDefine9').val();
                layer.confirm('确定要导出数据并下载到本地吗？', function (index) {
                    window.location.href = "/wc_api/getInventoryDow?str=后&cusCode=" + cusCode + "&material=" + material + "&invDef=" + invDef;
                    layer.close(index);
                });
                break;
        }
    })

})